package com.yc.demo.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;

public class Pushbox {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Pushbox window = new Pushbox();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Pushbox() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 462, 279);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(150, 10));
		frame.getContentPane().add(panel_1, BorderLayout.EAST);
		
		JButton btnNewButton = new JButton("开始游戏");
		
		btnNewButton.setPreferredSize(new Dimension(100, 23));
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("上一关");
		btnNewButton_1.setEnabled(false);
		
		btnNewButton_1.setPreferredSize(new Dimension(100, 23));
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("下一关");
		btnNewButton_2.setEnabled(false);
		
		btnNewButton_2.setPreferredSize(new Dimension(100, 23));
		panel_1.add(btnNewButton_2);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(20, 20, 0, 0));
		
//		JLabel lblNewLabel = new JLabel("New label");
//		lblNewLabel.setIcon(new ImageIcon(Pushbox.class.getResource("/com/yc/demo/ui/imgs/5.GIF")));
//		panel.add(lblNewLabel);
		
		////////////////////////////////////////////////////////////////
		// 初始化图片数组
		for (int i = 0; i < icons.length; i++) {
			icons[i] = new ImageIcon(Pushbox.class.getResource("/com/yc/demo/ui/imgs/" + i + ".GIF"));
		}
		
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++) {
				labelMap[i][j] = new JLabel("");
				labelMap[i][j].setIcon(icons[0]);
				panel.add(labelMap[i][j]);
			}
		}
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 开始游戏, 加载第一关
				loadMap();
				btnNewButton_1.setEnabled(true);
				btnNewButton_2.setEnabled(true);
			}
		});
		
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				level++;
				loadMap();
			}
		});
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(level == 1) return;
				level--;
				loadMap();
			}
		});
		
		// 自动调整窗口大小, 让所有的控件显式出来, 调整为最适大小
		frame.pack();
		
		// 添加移动监听器
		game.addMoveListener(new MoveListener() {
			@Override
			public void moved(MoveEvent e) {
				labelMap[e.y2][e.x2].setIcon(icons[e.value2]);
			}
		});
		
		Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
		    public void eventDispatched(AWTEvent event) {
		        if (((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) {
		        	KeyEvent e = (KeyEvent) event;
		        	if (e.getKeyCode() == KeyEvent.VK_UP) {
		        		game.up();
		        	} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
		        		game.down();
		        	} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
		        		game.left();
		        	} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
		        		game.right();
		        	}
		        }
		    }
		}, AWTEvent.KEY_EVENT_MASK);
		
	}
	
	public void loadMap() {
		int[][] map = game.getMap(level);
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++) {
				labelMap[i][j].setIcon(icons[map[i][j]]);
			}
		}
	}
	
	int level = 1;
	PushboxGame game = new PushboxGame();
	JLabel[][] labelMap = new JLabel[20][20];
	// 定义图片数组
	ImageIcon[] icons = new ImageIcon[10];
	


}
